package de.dopichaj.labrador.search.merge.pattern;

import java.util.Collection;
import java.util.SortedSet;

import de.dopichaj.labrador.search.hit.Hit;


public abstract class AbstractPattern {
    
    protected ScoreBooster booster;
    
    private static double shortLower = 10;
    private static double shortUpper = 20;
    {
        shortLower = Double.parseDouble(System.getProperty("shortLower", "10"));
        shortUpper = Double.parseDouble(System.getProperty("shortUpper", "20"));
        if (shortUpper <= shortLower) {
            throw new RuntimeException(
                "shortUpper must be greater than showtLower");
        }
    }
    public void setBooster(ScoreBooster booster) {
        this.booster = booster;
    }

    /**
     * Get a description of the pattern. You should override this.
     */
    public String getDescription() {
        return this.getClass().getCanonicalName();
    }
    
    /**
     * Apply this pattern to the given hit (and possibly its context).
     * 
     * Implementors can then call boostScore(s) on hits from tree.
     */
    abstract public void applyTo(Hit parent, SortedSet<Hit> children);

    /**
     * A ramp based on integer values.
     * <pre>
     * 1        _____
     *         /
     * 0 _____/
     *       l  u
     * </pre> 
     */
    protected double rampUp(double value, double lower, double upper) {
        return
            value <= lower
            ? 0
            : value >= upper
            ? 1
            : (value - lower) / (upper - lower);
    }
    
    protected double rampDown(double value, double lower, double upper) {
        return 1.0 - rampUp(value, lower, upper);
    }
    
    protected double trapezoid(double value, double a, double b, double c, double d) {
        return Math.min(rampUp(value, a, b), rampDown(value, c, d));
    }

    protected double shortness(int length) {
        return rampDown(length, shortLower, shortUpper);
    }
    
    protected double veryshortness(int length) {
        return rampDown(length, 3, 10);
    }
    
    protected double longness(int length) {
        return rampUp(length, 10, 50);
    }
    
    protected double sigmoid(double x, double scale, double shift) {
        return 1.0 / (1.0 + Math.exp(scale * -(x - shift)));
    }
    
    private double deltaScore(double s1, double s2) {
        return (s1 - s2) / Math.max(s1, s2);
    }
    
    protected double greaterScore(double s1, double s2) {
        if (s2 == 0.0 && s1 == 0.0) {
            return 0;
        }
        return rampUp(deltaScore(s1, s2), 0.05, 0.1);
    }
    
    protected double greaterOrEqualScore(double s1, double s2) {
        if (s2 == 0.0 && s1 == 0.0) {
            return 1;
        }
        return rampUp(deltaScore(s1, s2), -0.1, -0.05);
    }
    
    protected double equalScore(double s1, double s2) {
        if (s2 == 0.0 && s1 == 0.0) {
            return 1;
        }
        return trapezoid(deltaScore(s1, s2), -0.1, -0.05, 0.05, 0.1);
    }
    
    protected double isZeroScore(double s) {
        return sigmoid(s, -100, 0.1);
    }
    
    /* Pseudo-Boolean functions */
    protected double and(double ... args) {
        double min = args[0];
        
        for (double arg : args) {
            min = Math.min(min, arg);
        }
        return min;
    }

    protected void boostScore(Hit hit, double output, double membership) {
        booster.boostScore(hit, output, membership);
    }
    
    protected void boostScores(Collection<Hit> hits, double output, double membership) {
        booster.boostScores(hits, output, membership);
    }
}
/*
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/